package com.quan.weixin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.common.bean.PageBean;
import com.quan.common.core.MyBaseServiceImpl;
import com.quan.weixin.entity.WeixinOauthUser;
import com.quan.weixin.mapper.WeixinOauthUserMapper;
import com.quan.weixin.service.WeixinOauthUserService;

import cn.hutool.core.bean.BeanUtil;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
 * <p>
 * 微信用户表 服务实现类
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-11-12
 */
@Service
public class WeixinOauthUserServiceImpl extends MyBaseServiceImpl<WeixinOauthUserMapper, WeixinOauthUser> implements WeixinOauthUserService {

	@Autowired
	private WeixinOauthUserMapper mapper;
	
	@Override
	public IPage<WeixinOauthUser> selectMyPage(WeixinOauthUser entity, PageBean pageBean) {
		Page<WeixinOauthUser> page = new Page<>(pageBean.getPage(), pageBean.getLimit());
		
		// 排序
		if(StringUtils.isNotBlank(pageBean.getSortOrder()) && StringUtils.isNotBlank(pageBean.getSortName())) {
			List<OrderItem> orders = new ArrayList<OrderItem>();
			OrderItem orderItem = new OrderItem();
			orderItem.setColumn(pageBean.getSortName());
			orderItem.setAsc(!pageBean.getSortOrder().equalsIgnoreCase("DESC"));
			orders.add(orderItem);
			page.setOrders(orders);
		}
		return this.mapper.selectMyPage(page, entity);
	}
	
	@Override
	public WeixinOauthUser findByOpenId(String openid) {
		QueryWrapper<WeixinOauthUser> queryWrapper = new QueryWrapper<WeixinOauthUser>() ;
		queryWrapper.eq("openid", openid) ;
		
		return super.getOne(queryWrapper, false);
	}

	@Override
	public WeixinOauthUser findByMobile(String mobile) {
		QueryWrapper<WeixinOauthUser> queryWrapper = new QueryWrapper<WeixinOauthUser>() ;
		queryWrapper.eq("mobile", mobile) ;
		
		return super.getOne(queryWrapper, false);
	}

	@Override
	public void userSsubscribe(WxMpUser wxMpUser) {
		QueryWrapper<WeixinOauthUser> queryWrapper = new QueryWrapper<WeixinOauthUser>() ;
		queryWrapper.eq("openid", wxMpUser.getOpenId()) ;
		WeixinOauthUser oauthUser = super.getOne(queryWrapper, false);
		
		if(null != oauthUser) { 
			// 用户已存在（可能是访问H5授权获取的用户信息，并不是关注公众号获取用户信息），将该用户表示为关注
			oauthUser.setSubscribe(wxMpUser.getSubscribe()?1:0);
			oauthUser.setSubscribeScene(wxMpUser.getSubscribeScene());
			oauthUser.setSubscribeTime(wxMpUser.getSubscribeTime().toString());
			
			super.updateById(oauthUser);
		} else {
			// 用户为关注公众号，保持用户信息
			WeixinOauthUser user =  new WeixinOauthUser() ;
			BeanUtil.copyProperties(wxMpUser, user);
			user.setOpenid(wxMpUser.getOpenId());
			user.setCity(wxMpUser.getCity());
			user.setProvince(wxMpUser.getProvince());
			user.setHeadimgurl(wxMpUser.getHeadImgUrl());
			user.setPrivilege(StringUtils.join(wxMpUser.getPrivileges()));
			user.setUnionid(wxMpUser.getUnionId());
			
			super.save(user);
		}
	}

	@Override
	public void userUnSsubscribe(String openid) {
		QueryWrapper<WeixinOauthUser> queryWrapper = new QueryWrapper<WeixinOauthUser>() ;
		queryWrapper.eq("openid", openid) ;
		WeixinOauthUser oauthUser = super.getOne(queryWrapper, false);
		if(null != oauthUser) {
			oauthUser.setSubscribe(0);
			super.updateById(oauthUser);
		}
		
	}

}
